package com.ruoyi.bzjgl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.bzjgl.domain.Bzjtable;
import com.ruoyi.bzjgl.service.IBzjtableService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/system/bzjtable")
public class BzjtableController extends BaseController
{
    @Autowired
    private IBzjtableService bzjtableService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = bzjtableService.selectBzjtableList(bzjtable);
        return getDataTable(list);
    }


    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:list')")
    @GetMapping("/hzList")
    public TableDataInfo hzList(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = bzjtableService.selectBzjHzList(bzjtable);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bzjtable bzjtable)
    {
        List<Bzjtable> list = bzjtableService.selectBzjtableList(bzjtable);
        ExcelUtil<Bzjtable> util = new ExcelUtil<Bzjtable>(Bzjtable.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:query')")
    @GetMapping(value = "/{xh}")
    public AjaxResult getInfo(@PathVariable("xh") String xh)
    {
        return success(bzjtableService.selectBzjtableByXh(xh));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bzjtable bzjtable)
    {
        int i = bzjtableService.insertBzjtable(bzjtable);
        return toAjax(i);
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bzjtable bzjtable)
    {
        return toAjax(bzjtableService.updateBzjtable(bzjtable));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:bzjtable:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{xhs}")
    public AjaxResult remove(@PathVariable Integer[] xhs)
    {
        return toAjax(bzjtableService.deleteBzjtableByXhs(xhs));
    }


    //附件上传地址
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception{
        try{
            // 上传文件路径
            String fileFirstPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileLastPath = FileUploadUtils.upload(fileFirstPath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION).replace("/upload","");
            AjaxResult ajax = AjaxResult.success();
            HashMap<String,Object> fileTable = new HashMap<>();
            fileTable.put("fileFirstPath", fileFirstPath);
            fileTable.put("fileLastPath", fileLastPath);
            fileTable.put("newFileName", FileUtils.getName(fileLastPath));
            fileTable.put("originalFilename", file.getOriginalFilename());
            List<HashMap<String,Object>> fileTables = new ArrayList<>();
            fileTables.add(fileTable);
            ajax.put("fileTables",fileTables);
            return ajax;
        }catch (Exception e){
            return   AjaxResult.error(e.getMessage());
        }
    }

}
